package com.yang.service.adminimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketIssue;
import com.yang.bean.MarketIssueExample;
import com.yang.bean.common.CommonData;
import com.yang.bean.yvo.IssueListBO;
import com.yang.mapper.MarketIssueMapper;
import com.yang.service.admin.AdminIssueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.7 20:30
 * @description:
 */
@Service
public class AdminIssueServiceImpl implements AdminIssueService {
    @Autowired
    MarketIssueMapper marketIssueMapper;
    @Override
    public CommonData selectIssue(IssueListBO issueListBO) {
        PageHelper.startPage(issueListBO.getPage(),issueListBO.getLimit());
        String sort = issueListBO.getSort();
        String order = issueListBO.getOrder();
        MarketIssueExample example = new MarketIssueExample();
        example.setOrderByClause(sort+" "+order);
        MarketIssueExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(issueListBO.getQuestion())) {
            criteria.andQuestionLike("%"+issueListBO.getQuestion()+"%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(example);
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(marketIssues);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public MarketIssue updateIssue(MarketIssue marketIssue) {
        marketIssue.setUpdateTime(new Date());
        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketIssue.getId());
        marketIssueMapper.updateByExampleSelective(marketIssue, example);
        return marketIssue;
    }

    @Override
    public void delete(MarketIssue marketIssue) {
        marketIssue.setUpdateTime(new Date());
        marketIssue.setDeleted(true);
        MarketIssueExample example = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketIssue.getId());
        marketIssueMapper.updateByExampleSelective(marketIssue, example);
    }

    @Override
    public MarketIssue create(MarketIssue marketIssue) {
        marketIssue.setAddTime(new Date());
        marketIssueMapper.insertSelective(marketIssue);
        Integer id = marketIssue.getId();
        MarketIssue marketIssue1 = marketIssueMapper.selectByPrimaryKey(id);
        return marketIssue1;
    }
}
